package com.archer.task02;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.util.Objects;

@SpringBootApplication
public class Task02Application {

    private static final OkHttpClient httpClient = new OkHttpClient();

    public static void main(String[] args) throws IOException {
        String url = "http://localhost:8801/";
        String as = getAsString(url);
        System.out.println("url: " + url + System.lineSeparator() + "AS: " + as);
    }

    private static String getAsString(String url) throws IOException {
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return Objects.requireNonNull(response.body()).toString();
    }
}
